//
//  HomeDataModel.m
//  huashiedu
//
//  Created by oceans on 2018/8/26.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "HomeDataModel.h"
#import <UIKit/UIKit.h>

@implementation HomeCourseModel

- (instancetype)initWithDic:(NSDictionary *)dic textWidth:(CGFloat)textwidth fontsize:(CGFloat)fontsize {
    self = [super init];
    if (self) {
        
        [self setup:dic textwidth:textwidth fontsize:fontsize];
    }
    return self;
}

- (void)setup:(NSDictionary*)dic textwidth:(CGFloat)textwidth fontsize:(CGFloat)fontsize {
    
    __id = [[dic objectForKey:@"id"] intValue];
    _title = [dic objectForKey:@"title"];
    _banner = [dic objectForKey:@"banner"];
    _thumb = [dic objectForKey:@"thumb"];
    _listorder = [[dic objectForKey:@"listorder"] intValue];
    _link = [dic objectForKey:@"link"];
    
    CGRect titleRect = [_title boundingRectWithSize:CGSizeMake(textwidth, 100) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontsize]} context:nil];
    self.size = CGSizeMake(textwidth, titleRect.size.height);
}

@end

@implementation HomeDataModel

- (instancetype)initWithDic:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        __id = [[dic objectForKey:@"id"] intValue];
        _name = [dic objectForKey:@"name"];
        NSArray *recommends = [dic objectForKey:@"recommends"];
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:recommends.count];
        for (int i = 0; i < recommends.count; i ++) {
            
            NSDictionary *rec = recommends[i];
            HomeCourseModel *model = [[HomeCourseModel alloc] initWithDic:rec textWidth:((i == 0) ? (ScreenWidth - 26) : (ScreenWidth - 39) * 0.5) fontsize:((i == 0) ? 16 : 14)];
            if (i == 0) {
                model.size = CGSizeMake(model.size.width, model.size.height + 13 + ((ScreenWidth - 26) * 0.366));
            }else {
                model.size = CGSizeMake(model.size.width, model.size.height + 13 + ((ScreenWidth - 39) * 0.5 * 0.57));
            }
            [array addObject:model];
        }
        _recommends = array;
    }
    return self;
}

@end
